
foreach var in ///
cred_ratio /// //credit/(credit+debit) ratio in general
cr_dur cr_nondur cr_tempt /// //expenditure supercategories
cr_hi cr_ca cr_transp /// //durables
cr_groc cr_fuel cr_rmf cr_pharm cr_recr cr_char cr_sa /// //non-durables
cr_alc cr_lott_exp cr_lott_char_exp cr_gambling_exp cr_game /// //temptations
{
bysort cohold_spell_cat_id: egen `var'_spell = mean(`var') //mean within each coholding spell category
}

foreach y in cr_utiliz ccard_lim_rel ///
cred_ratio ///
cr_groc cr_rmf cr_hi  {

	if "`y'" == "cr_utiliz" {
	local title "Credit card balance relative to limit"
	local format "%9.2fc"
	}
	else if "`y'" == "ccard_lim_rel" {
	local title "Credit card limit relative to average"
	local format "%9.2fc"
	}
	else if "`y'" == "cred_ratio" {
	local title "Credit ratio"
	local format "%9.3fc"
	}
	else if "`y'" == "cr_groc" {
	local title "Credit ratio for groceries"
	local format "%9.3fc"
	}
	else if "`y'" == "cr_rmf" {
	local title "Credit ratio for ready-made food"
	local format "%9.3fc"
	}
	else if "`y'" == "cr_hi" {
	local title "Credit ratio for home improvements"
	local format "%9.3fc"
	}

	local category "ind_emy_seq"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

		foreach var in `category' {
		if "`var'" == "ind_emy_seq" {
			local min 1
			local max 31
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	scatter mean n if subcategory != "", ///
	mcolor(blue) mstyle(p1) ///
	title("`title'", place(left) size(large)) ///
	ytitle("") ///
	xtitle("th day in month", size(large)) ///
	ylabel(, labsize(medium) angle(0) format("`format'")) /// 
	xlabel(0 5 10 15 20 25 30, labsize(medium) angle(0))  ///
	xscale(range(0.25 31.75)) ///
	legend(off)  
	 
drop subcategory mean lqt uqt n 
drop min max  	 
graph export "Figures/CohExpl_Mental_month_`y'.pdf", replace
graph export "Figures/CohExpl_Mental_month_`y'.png", replace
}


foreach var in ///
cred_ratio /// //ratio in general
cr_dur cr_nondur cr_tempt ///expenditure supercategories
cr_hi cr_ca cr_transp /// //durables
cr_groc cr_fuel cr_rmf cr_pharm cr_recr cr_char cr_sa /// //non-durables
cr_alc cr_lott_exp cr_lott_char_exp cr_gambling_exp cr_game /// //temptations
 {
bysort cohold_spell_cat: egen mean = mean(`var'_spell) if ///
				coholder==1 & cohold_spell_cat_uoa==1 //subset to coholders and 1 observation per cohold spell category
bysort cohold_spell_cat: egen std = sd(`var'_spell) if ///
				coholder==1 & cohold_spell_cat_uoa==1
bysort cohold_spell_cat: egen obs = count(dum) if ///
				coholder==1 & cohold_spell_cat_uoa==1
gen se = std/sqrt(obs) if 	coholder==1 & cohold_spell_cat_uoa==1
bysort cohold_spell_cat: egen n = seq() if ///
				coholder==1 & cohold_spell_cat_uoa==1

if "`var'" == "cred_ratio" {
local title "Credit ratio"
local format "%9.3fc"
}
else if "`var'" == "cr_dur" {
local title "Credit ratio for durables"
local format "%9.3fc"
}
else if "`var'" == "cr_nondur" {
local title "Credit ratio for non-durables"
local format "%9.3fc"
}
else if "`var'" == "cr_tempt" {
local title "Credit ratio for temptations"
local format "%9.3fc"
}
else if "`var'" == "cr_hi" {
local title "Credit ratio for home improvements"
local format "%9.3fc"
}
else if "`var'" == "cr_ca" {
local title "Credit ratio for clothes and accessories"
local format "%9.3fc"
}
else if "`var'" == "cr_transp" {
local title "Credit ratio for transportation"
local format "%9.3fc"
}
else if "`var'" == "cr_groc" {
local title "Credit ratio for groceries"
local format "%9.3fc"
}
else if "`var'" == "cr_fuel" {
local title "Credit ratio for fuel"
local format "%9.3fc"
}
else if "`var'" == "cr_rmf" {
local title "Credit ratio for ready-made food"
local format "%9.3fc"
}
else if "`var'" == "cr_pharm" {
local title "Credit ratio for pharmaceuticals"
local format "%9.3fc"
}
else if "`var'" == "cr_recr" {
local title "Credit ratio for recreation"
local format "%9.3fc"
}
else if "`var'" == "cr_char" {
local title "Credit ratio for charities"
local format "%9.3fc"
}
else if "`var'" == "cr_sa" {
local title "Credit ratio for sports and activities"
local format "%9.3fc"
}
else if "`var'" == "cr_alc" {
local title "Credit ratio for alcohol"
local format "%9.3fc"
}
else if "`var'" == "cr_lott_exp" {
local title "Credit ratio for lottery expenditures"
local format "%9.3fc"
}
else if "`var'" == "cr_lott_char_exp" {
local title "Credit ratio for lottery (charity) expenditures"
local format "%9.3fc"
}
else if "`var'" == "cr_gambling_exp" {
local title "Credit ratio for gambling expenditures"
local format "%9.3fc"
}
else if "`var'" == "cr_game" {
local title "Credit ratio for gaming expenditures"
local format "%9.3fc"
}
serrbar mean se cohold_spell_cat if n ==1, /// 
mvopts(mcolor(blue)) ///
scale(1.64) ///
title("`title'", place(left) size(large)) ///
ytitle("") ///
xtitle("Point in coholding cycle", size(large)) ///
ylabel(, labsize(large) angle(0) format("`format'")) ///
xlabel(0 "No cohold day" 1 "Start" 2 "First 1/3" 3 "Intermediate 1/3" 4 "Last 1/3" 5 "End", ///
	labsize(medium)) ///
xscale(range(-0.25 5.25)) 
graph export "Figures/CohExpl_Mental_`var'.pdf", replace
graph export "Figures/CohExpl_Mental_`var'.png", replace
drop mean std obs se n
}
